<?php


namespace api\portal\controller;


use cmf\controller\RestBaseController;
use think\Db;
use think\Exception;
use think\Request;

class NoticeController extends RestBaseController
{
    /**
     * 列表
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function index()
    {
        $requst = $this->request;
        $list_rows = $requst->param("list_rows");
        $noticeTitle = $this->getChineseGetValue($_GET['noticeTitle']);
        $addUser = $this->getChineseGetValue($_GET["addUser"]);
        $userId = $requst->param("userId");
        $token = $requst->param("token");

        $this->LogInOrNot($userId, $token);
//        $userId = $this->getUserId();

        $data = Db::name("notice")->alias("n")->field("n.id,n.notice_title,FROM_UNIXTIME(n.add_time) add_time,u.is_read")
            ->join("notice_user u", "n.id = u.notice_id")
            ->join("user us", "us.id = n.add_user")
            ->where("notice_title", 'like', '%' . $noticeTitle . '%')
            ->where("us.user_nickname", 'like', '%' . $addUser . '%')
            ->where("u.user_id", $userId)
            ->order("u.is_read asc,n.add_time desc")
            ->paginate($list_rows, false, ['query' => request()->param()]);
        if (!empty($data)) {
            $response = ['list' => $data];
        } else {
            $response = $data;
        }
        $this->success('请求成功!', $response);

    }

    /**
     * 详情
     * @param $id
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function details($id)
    {
        $requst = $this->request;
        $userId = $requst->param("userId");
        $token = $requst->param("token");
        $this->LogInOrNot($userId, $token);
        if (intval($id) === 0) {
            $this->error('无效的消息id！');
        } else {
            $data = Db::name("notice")->alias("n")
                ->field("n.notice_content,n.notice_title,n.id,FROM_UNIXTIME(n.add_time) add_time,us.user_nickname")
                ->join("user us", "n.add_user = us.id")
                ->where('n.id', $id)->find();
            if (empty($data)) {
                $this->error('消息通知不存在！');
            } else {
                $this->success('请求成功!', $data);
            }
        }
    }
    public function is_read(){
        try{
            $requst = $this->request;
            $userId = $requst->post("userId");
            $token = $requst->post("token");
            $this->LogInOrNot($userId, $token);
            $notice_id = $requst->post("id");
            $flag = Db::name("notice_user")->where(["notice_id"=>$notice_id,"user_id"=>$userId])->update(["is_read"=>"T","read_time"=>time()]);
            if ($flag){
                $this->success("成功",$flag);
            }else{
                $this->error("失败");
            }

        }catch(Exception $e){
            $this->error($e->getMessage());
        }
    }


}